package ro.xblue.translator;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;



/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: :D application</p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: Romania Development Gateway </p>
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: ChangeLanguageTag.java,v 1.1 2004/10/24 23:37:02 mihaipostelnicu Exp $
 */
public class ChangeLanguageTag extends TagSupport
{
      private boolean showGoButton;
      private boolean showLabel;

      public void setShowLabel( boolean showLabel )
      {
            this.showLabel = showLabel;
      }

      public boolean isShowLabel()
      {
            return showLabel;
      }

      public void setShowGoButton( boolean showGoButton )
      {
            this.showGoButton = showGoButton;
      }

      public boolean isShowGoButton()
      {
            return showGoButton;
      }

      public void release()
      {
            super.release();
      }

      public int doEndTag() throws JspException
      {
            ServletContext application = pageContext.getServletContext();
            HttpServletRequest request = ( HttpServletRequest ) pageContext.getRequest();
            String ctx = request.getContextPath();

            LanguageBean clang = TranslatorUtils.
                  getCurrentLanguage( request, application );

//            Translator translator = ( Translator ) application.
//                  getAttribute( TranslatorConstants.BLUE_TRANSLATOR );
            Translator_DB translator_db = null;
//            if ( translator == null )
//            {
                  translator_db = ( Translator_DB ) application.
                        getAttribute( TranslatorConstants.DB_TRANSLATOR );
//            }

            String goLabel = "GO";
//            if ( translator != null )
//            {
//                  String tmp = translator.getMessage( clang.language, "translator", "form.bgo" );
//                  if ( tmp != null ) goLabel = tmp;
//            }
//            else if ( translator_db != null )
//            {
                  String tmp = translator_db.getMessage( clang.language, "translator", "form.bgo" );
                  if ( tmp != null ) goLabel = tmp;
//            }

            StringBuffer sb = new StringBuffer();
            sb.append( "<table class=\"lngtable\">" ).
                  append( "<form name=\"lngform\" method=\"POST\" action=\"" ).
                  append( ctx ).
                  append( "/lang.change" );
            String key = request.getParameter( "key" );
            if ( key != null )
            {
                  sb.append( "?key=" ).
                        append( key );
            }
            sb.append( "\">" ).
                  append( "<tr><td class=\"lngtdwhite\">" );
            if ( showLabel )
            {
                  String label = "form.chglabel";
//                  if ( translator != null )
//                  {
//                        label = translator.getMessage( clang.language, "translator", "form.chglabel" );
//                  }
//                  else if ( translator_db != null )
//                  {
                        //label = translator_db.getMessage( clang.language, "translator", "form.chglabel" );
//                  }
//                  if ( label == null )
                  //{
                        //label = "form.chglabel";
                  //}
                  HttpSession session=request.getSession();
                  boolean editTags= (session.getAttribute( ShowEditButtonAction.FLAG )!=null);
                  if(editTags)
                  sb.append( ShowPageMessageTag.get_linked_message(pageContext,label) +" : ");
                  else
                  	sb.append( ShowPageMessageTag.get_message(pageContext,label) +" : ");
            }
            sb.append( "<select class=\"lngtdblack\" name=\"lang\" onchange=\"this.form.submit();\">" );
            LanguageBean[] avail_langs = ( LanguageBean[] ) application.
                  getAttribute( TranslatorConstants.BLUE_AVAIL_LANGS );
            if ( avail_langs == null )
            {
                  System.err.println( "\t\t! : translator not started properly" );
                  return EVAL_PAGE;
            }
            for ( int i = 0; i < avail_langs.length; i++ )
            {
                  String disp_name = "";
//                  if ( translator != null )
//                  {
//                        disp_name = translator.getMessage(
//                              clang.language,
//                              "translator",
//                              "lng.avail." + avail_langs[ i ].language );
//                  }
//                  else if ( translator_db != null )
//                  {
                        disp_name = translator_db.getMessage(
                              clang.language,
                              "translator",
                              "lng.avail." + avail_langs[ i ].language );
//                  }
                  if ( disp_name == null )
                  {
                        disp_name = avail_langs[ i ].locale.getDisplayLanguage();
                  }
                  boolean selected = ( avail_langs[ i ].language.equals( clang.language ) );

                  sb.append( "<option " ).
                        append( selected ? "selected=\"true\" " : "" ).
                        append( "value=\"" ).
                        append( avail_langs[ i ].language ).
                        append( "\">" ).
                        append( disp_name ).
                        append( "</option>" );
            }
            sb.append( "</select>" );
            if ( showGoButton )
            {
                  sb.append( "<input type=\"submit\" value=\"" ).
                        append( goLabel ).
                        append( "\" />" );
            }
            sb.append( "</td></tr></form>" ).
                  append( "</table>" );

            JspWriter writer = pageContext.getOut();
            try
            {
                  writer.println( sb.toString() );
            }
            catch ( IOException ioEx )
            {
                  System.err.println( ioEx );
            }

            this.showGoButton = false;
            this.showLabel = false;

            return EVAL_PAGE;
      }

      public static String[][] getAvailableLanguages(
            HttpServletRequest request,
            ServletContext application
            )
      {

            LanguageBean clang = TranslatorUtils.
                  getCurrentLanguage( request, application );

//            Translator translator = ( Translator ) application.
//                  getAttribute( TranslatorConstants.BLUE_TRANSLATOR );
            Translator_DB translator_db = null;
//            if ( translator == null )
//            {
                  translator_db = ( Translator_DB ) application.
                        getAttribute( TranslatorConstants.DB_TRANSLATOR );
//            }

            LanguageBean[] avail_langs = ( LanguageBean[] ) application.
                  getAttribute( TranslatorConstants.BLUE_AVAIL_LANGS );
            if ( avail_langs == null )
            {
                  System.err.println( "\t\t! : translator not started properly" );
                  return null;
            }

            ArrayList items = new ArrayList();
            for ( int i = 0; i < avail_langs.length; i++ )
            {
                  String disp_name = "";
//                  if ( translator != null )
//                  {
//                        disp_name = translator.getMessage(
//                              clang.language,
//                              "translator",
//                              "lng.avail." + avail_langs[ i ].language );
//                  }
//                  else if ( translator_db != null )
//                  {
                        disp_name = translator_db.getMessage(
                              clang.language,
                              "translator",
                              "lng.avail." + avail_langs[ i ].language );
//                  }
                  if ( disp_name == null )
                  {
                        disp_name = avail_langs[ i ].locale.getDisplayLanguage();
                  }

                  String[] item = new String[]{
                        avail_langs[ i ].language,
                        disp_name
                  };
                  items.add( item );
            }

            String[][] result = null;
            if ( items.size() > 0 )
            {
                  result = new String[ items.size() ][ 2 ];
                  for ( int i = 0; i < items.size(); i++ )
                  {
                        result[ i ] = ( String[] ) items.get( i );
                  }
            }
            return result;
      }

      public int doStartTag() throws JspException
      {
            return SKIP_BODY;
      }
}
